package 多线程.交替输出;


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * @author : K k
 * @date : 23:04 2020/8/11
 * 创建两个阻塞队列
 */
public class Ans2BlockQueue {
    //创建两个阻塞队列
    static BlockingQueue<String> q1= new ArrayBlockingQueue(1);
    static BlockingQueue<String> q2= new ArrayBlockingQueue(1);

    public static void main(String[] args) {
        char[] a1 = "1234567".toCharArray();
        char[] a2 = "ABCDEFG".toCharArray();

            new Thread(() -> {
            for (char c : a1) {
                System.out.println(c);
                try {
                    q1.put("ok");
                    q2.take();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(() -> {
            for (char c : a2) {
                try {
                    q1.take();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println(c);
                try {
                    q2.put("ok");
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}
